package cn.sf.sufingvideoprovider.serviceImpl;

import cn.sf.pojo.Video;
import cn.sf.service.VideoService;
import cn.sf.sufingvideoprovider.dao.CollectMapper;
import cn.sf.sufingvideoprovider.dao.VideoMapper;
import cn.sf.vo.*;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class VideoServiceImpl implements VideoService {

    @Resource
    private VideoMapper videoMapper;
    @Resource
    private CollectMapper collectMapper;

    @Override
    @RequestMapping("/queryVideoAndUser")
    public Video queryVideoAndUser(long id) {
        return videoMapper.queryVideoAndUser(id);
    }

    @Override
    @RequestMapping("/queryOtherVideoList")
    public List<Video> queryOtherVideoList(long classify) {
        return videoMapper.queryOtherVideoList(classify);
    }

    @Override
    @RequestMapping("/updVideoPlayNumber")
    public boolean updVideoPlayNumber(long videoId) {
        Video video = videoMapper.queryVideoNumberInfo(videoId);    //查询视频信息
        video.setVideoPlaynumber(video.getVideoPlaynumber()+1);     //修改视频播放量
        if(videoMapper.updVideoNumber(video)>0){
            return true;
        }
        return false;
    }

    @Override
    @RequestMapping("/updVideoLikeNumber")
    public boolean updVideoLikeNumber(long videoId) {
        Video video = videoMapper.queryVideoNumberInfo(videoId);    //查询视频信息
        video.setVideoLikenumber(video.getVideoLikenumber()+1);     //修改视频点赞量
        if(videoMapper.updVideoNumber(video)>0){
            return true;
        }
        return false;
    }

    @Override
    @RequestMapping("/updVideoReport")
    public boolean updVideoReport(long videoId) {
        Video video = videoMapper.queryVideoNumberInfo(videoId);    //查询视频信息
        video.setVideoReport(video.getVideoReport()+1);             //修改视频举报量
        if(videoMapper.updVideoNumber(video)>0){
            return true;
        }
        return false;
    }

    @Override
    @RequestMapping("/queryIsOrNotCollect")
    public boolean queryIsOrNotCollect(long userId, long videoId) {
        if(collectMapper.queryIsOrNotCollect(userId, videoId)>0){
            return true;
        }
        return false;
    }

    /**
     * 实现事务操作，将为该视频收藏量+1，添加到收藏表中同步执行
     * 实现事务操作，将为该视频收藏量-1，删除收藏表中的数据同步执行
     * @param userId    登录用户ID
     * @param videoId   视频ID
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @RequestMapping("/VideoCollect")
    public String VideoCollect(long userId, long videoId) throws Exception{
        try {
            if(collectMapper.queryIsOrNotCollect(userId,videoId)>0){
                //实现视频收藏量减1
                Video video = videoMapper.queryVideoNumberInfo(videoId);
                video.setVideoCollectnumber(video.getVideoCollectnumber()-1);
                if(videoMapper.updVideoNumber(video)>0){
                    //实现添加到收藏表中
                    if(collectMapper.delCollectVideo(userId,videoId)>0){
                        return "取消收藏成功";        //俩者执行成功返回true
                    }
                    throw new RuntimeException("取消收藏失败");
                }
                throw new RuntimeException("视频收藏量减少失败");
            }else{
                //实现为视频被收藏量+1
                Video video = videoMapper.queryVideoNumberInfo(videoId);
                video.setVideoCollectnumber(video.getVideoCollectnumber()+1);
                if(videoMapper.updVideoNumber(video)>0){
                    //实现添加到收藏表中
                    if(collectMapper.addCollectVideo(userId,videoId)>0){
                        return "添加收藏成功";        //俩者执行成功返回true
                    }
                    throw new RuntimeException("添加收藏失败");
                }
                throw new RuntimeException("视频收藏量增加失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//抛了异常后是会回滚的
            return "执行失败";
        }
    }


    @Override
    @RequestMapping("/get")
    public List<VideoVo> get(Integer id) {
        List<VideoVo> all = videoMapper.getAll(id);
        return all;
    }

    @Override
    @RequestMapping("/getAllComment")
    public List<VideoComment> getAllComment(Integer commentVideoId) {
        List<VideoComment> list=videoMapper.getAllComment(commentVideoId);
        return list;
    }



    @Override
    @PostMapping("/getVideoAll")
    public String getVideoAll(String videoTitle,Integer videoUserId,Integer pageNo) {
        //设置分页的 当前页和页容量
        PageHelper.startPage(pageNo,2);
        List<VideoAll> videoAll = videoMapper.getVideoAll(videoTitle,videoUserId,pageNo);
        //将查询出来的数据放进分页工具类中进行处理
        PageInfo<VideoAll> pageInfo=new PageInfo<VideoAll>(videoAll);
        String s = JSON.toJSONString(pageInfo);
        return s;
    }

    @Override
    @PostMapping("/updateVideo")
    public int updateVideo(@RequestBody Video video) {
        return videoMapper.updateVideo(video);
    }

    @Override
    @PostMapping("/deleteVideo")
    public int deleteVideo(Integer videoId) {
        return videoMapper.deleteVideo(videoId);
    }

    @Override
    @RequestMapping("/getVideoDate")
    public List<VideoDate> getVideoDate(Integer userId) {
        return videoMapper.getVideoDate(userId);
    }

    @Override
    @RequestMapping("/getComment")
    public List<CommentAll> getComment(Integer commentVideoId) {
        return videoMapper.getComment(commentVideoId);
    }

    @Override
    @PostMapping("/deleteComment")
    public int deleteComment(Integer commentId) {
        return videoMapper.deleteComment(commentId);
    }

    @Override
    @RequestMapping("/getVideoIdMAX")
    public int getVideoIdMAX(long id) {
        return videoMapper.getVideoIdMAX(id);
    }



    @Override
    @RequestMapping("/addVideo")
    public int addVideo(@RequestBody Video video) {
        int vid=this.getVideoIdMAX(video.getVideoUserId());
        video.setVideoVid(vid+1);
        int i=videoMapper.addVideo(video);
        return i;
    }
}
